package logic

import (
    // "fmt"
    "e4ting/internal/model"
    "e4ting/pb/e4ting"
    structpb "google.golang.org/protobuf/types/known/structpb"
)

func GetPage(in *e4ting.SearchRequest) (int, int, string) {
    // todo: add your logic here and delete this line
    if in == nil{
        return 10, 0, ""
    }
    size := max(in.Size, 5)
    page := max(in.Page, 1) - 1
    limit,offset := int(size),int(size * page)
    return limit,offset,in.Keyword
}

func ToTypeData(item model.Types) *e4ting.TypeData {
    return &e4ting.TypeData {
        Id     : int32(item.Id),
        Name   : item.Name,
        Detail : item.Detail,
        Status : item.Status,
        Removed : item.Removed,
        Createtime: item.CreateTime.Format("2006-01-02 15:04:05"),
    }
}

func ToAccountData(item model.Moneys) *e4ting.AccountData {
    return &e4ting.AccountData {
        Id     : int32(item.Id),
        Name   : item.Name,
        TypeID : int32(item.Types),
        Money  : int32(item.Money),
        Uuid   : item.Uuid,
        Detail : item.Detail,
        Status : item.Status,
        Removed : item.Removed,
        Createtime: item.CreateTime.Format("2006-01-02 15:04:05"),
    }
}
func ToAccountList(items []model.Moneys) (ret []*e4ting.AccountData) {
    for _, data := range items {
        ret = append(ret, ToAccountData(data))
    }
    return ret
}

func ToTypeList(items []model.Types) (ret []*e4ting.TypeData) {
    // var ret []*structpb.Value
    for _, data := range items {
        ret = append(ret, ToTypeData(data))
    }
    return ret
}

func ToUtxoData(item model.Utxo) *e4ting.UtxoData {
    return &e4ting.UtxoData {
        Money  : int32(item.Money),
        TypeID : int32(item.Types),
        Uid    : int32(item.Uid),
        Uuid   : item.Uuid,
        Detail : item.Detail,
        Status : item.Status,
        Removed : item.Removed,
        Createtime: item.CreateTime.Format("2006-01-02 15:04:05"),
    }
}
func ToUtxoList(items []model.Utxo) (ret []*e4ting.UtxoData) {
    for _, data := range items {
        ret = append(ret, ToUtxoData(data))
    }
    return ret
}

func ToList(items []model.Dict) ([]*structpb.Value) {
    var ret []*structpb.Value
    for _, data := range items {
        // fmt.Printf("%+v\n", data)
        st, err := structpb.NewStruct(data)
        if err != nil {
            return nil
        }
        value := &structpb.Value{}
        value.Kind = &structpb.Value_StructValue{
            StructValue: st,
        }
        ret = append(ret, value)
    }
    return ret
}

func RToList( items []*structpb.Value ) ([]model.Dict) {
    var ret []model.Dict
    for _,item := range items{
        // fmt.Println(i)
        // fmt.Printf("%+v\n", item.GetStructValue().String())
        // fmt.Printf("%+v\n", item.GetStructValue().AsMap())
        ret = append(ret, item.GetStructValue().AsMap())
    }
    return ret
}